﻿namespace ImageProcessing.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// histogram matching form
    /// </summary>
    public partial class HistogramsMatchingForm : Form
    {
        /// <summary>
        /// image 1
        /// </summary>
        private DImage image1 = new DImage();

        /// <summary>
        /// image 2
        /// </summary>
        private DImage image2 = new DImage();

        /// <summary>
        /// result image
        /// </summary>
        private DImage result = new DImage();

        /// <summary>
        /// Histogram1 for image1
        /// </summary>
        private HistogramChart histo1 = new HistogramChart();

        /// <summary>
        /// Histogram2 for image2
        /// </summary>
        private HistogramChart histo2 = new HistogramChart();

        /// <summary>
        /// Histogram3 for image3
        /// </summary>
        private HistogramChart histo3 = new HistogramChart();

        /// <summary>
        /// Initializes a new instance of the <see cref="HistogramsMatchingForm"/> class.
        /// </summary>
        public HistogramsMatchingForm()
        {
            this.InitializeComponent();

            if (Main.ImageList.Count == 0)
            {
                return;
            }

            #region insulize hitogram
            pnlHisto1.Controls.Add(histo1.HistoChart);
            PnlHisto2.Controls.Add(histo2.HistoChart);
            PnlHisto3.Controls.Add(histo3.HistoChart);
            pnlHisto1.Controls.Add(histo1.CheckBlue);
            pnlHisto1.Controls[pnlHisto1.Controls.Count - 1].BringToFront();

            pnlHisto1.Controls.Add(histo1.CheckGray);
            pnlHisto1.Controls[pnlHisto1.Controls.Count - 1].BringToFront();

            pnlHisto1.Controls.Add(histo1.CheckGreen);
            pnlHisto1.Controls[pnlHisto1.Controls.Count - 1].BringToFront();

            pnlHisto1.Controls.Add(histo1.CheckRed);
            pnlHisto1.Controls[pnlHisto1.Controls.Count - 1].BringToFront();

            PnlHisto2.Controls.Add(histo2.CheckBlue);
            PnlHisto2.Controls[PnlHisto2.Controls.Count - 1].BringToFront();

            PnlHisto2.Controls.Add(histo2.CheckGray);
            PnlHisto2.Controls[PnlHisto2.Controls.Count - 1].BringToFront();

            PnlHisto2.Controls.Add(histo2.CheckGreen);
            PnlHisto2.Controls[PnlHisto2.Controls.Count - 1].BringToFront();

            PnlHisto2.Controls.Add(histo2.CheckRed);
            PnlHisto2.Controls[PnlHisto2.Controls.Count - 1].BringToFront();

            PnlHisto3.Controls.Add(histo3.CheckBlue);
            PnlHisto3.Controls[PnlHisto3.Controls.Count - 1].BringToFront();
            
            PnlHisto3.Controls.Add(histo3.CheckGray);
            PnlHisto3.Controls[PnlHisto3.Controls.Count - 1].BringToFront();

            PnlHisto3.Controls.Add(histo3.CheckGreen);
            PnlHisto3.Controls[PnlHisto3.Controls.Count - 1].BringToFront();

            PnlHisto3.Controls.Add(histo3.CheckRed);
            PnlHisto3.Controls[PnlHisto3.Controls.Count - 1].BringToFront();

            #endregion

            foreach (DImage img in Main.ImageList)
            {
                this.CmbFirstImage.Items.Add(img.Name);
                this.CmbSecondImage.Items.Add(img.Name);
            }

            this.CmbFirstImage.SelectedIndex = 0;
            if (Main.ImageList.Count > 1)
            {
                this.CmbSecondImage.SelectedIndex = 1;
            }
            else
            {
                this.CmbSecondImage.SelectedIndex = 0;
            }

            this.image1.ImageStructArray = Main.ImageList[this.CmbFirstImage.SelectedIndex].ImageStructArray;
            this.PcbFirstImage.Image = Operations.DisplayMatrix( Main.ImageList[this.CmbFirstImage.SelectedIndex].ImageStructArray);
            this.histo1.Updatehisto(Main.ImageList[this.CmbFirstImage.SelectedIndex]);
            this.image2.ImageStructArray = Main.ImageList[this.CmbSecondImage.SelectedIndex].ImageStructArray;
            this.PcbSecondImage.Image = Operations.DisplayMatrix(Main.ImageList[this.CmbSecondImage.SelectedIndex].ImageStructArray);
            this.histo2.Updatehisto(Main.ImageList[this.CmbSecondImage.SelectedIndex]);

        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CmbFirstImage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CmbFirstImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.image1.ImageStructArray = Main.ImageList[this.CmbFirstImage.SelectedIndex].ImageStructArray;
          
            this.PcbFirstImage.Image = Operations.DisplayMatrix(Main.ImageList[this.CmbFirstImage.SelectedIndex].ImageStructArray);
            this.histo1.Updatehisto(Main.ImageList[this.CmbFirstImage.SelectedIndex]);
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CmbSecondmage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CmbSecondmage_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.image2.ImageStructArray = Main.ImageList[this.CmbSecondImage.SelectedIndex].ImageStructArray;

            this.PcbSecondImage.Image = Operations.DisplayMatrix(Main.ImageList[this.CmbSecondImage.SelectedIndex].ImageStructArray);
            this.histo2.Updatehisto(Main.ImageList[this.CmbSecondImage.SelectedIndex]);
        }

        /// <summary>
        /// Handles the Click event of the BtnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOk_Click(object sender, EventArgs e)
        {
            Main.ImageList[Main.CurrImage].UpdateImage(Operations.ConvertBitmapToStructure(this.result.ImageBitmap));
            this.Close();
        }

        /// <summary>
        /// Handles the Enter event of the groupBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GroupBox1_Enter(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Load event of the HistogramsMatchingForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void HistogramsMatchingForm_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the BtnRun control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnRun_Click(object sender, EventArgs e)
        {
          DateTime before = DateTime.Now;
         this.result.ImageBitmap = Equalization.HistogramMatching(Main.ImageList[this.CmbFirstImage.SelectedIndex].ImageBitmap, Main.ImageList[this.CmbSecondImage.SelectedIndex].ImageBitmap);
         this.PcbResult.Image = this.result.ImageBitmap;
            
          TimeSpan diff = DateTime.Now - before;
            MessageBox.Show(diff.TotalMilliseconds.ToString());
            this.result.ImageStructArray = Operations.ConvertBitmapToStructure(this.result.ImageBitmap);
            result.Height1 = result.ImageStructArray.GetLength(0);
            result.Width1 = result.ImageStructArray.GetLength(1);
            this.histo3.Updatehisto(result);
        }
    }
}
